#include "ast.h"

AST* AST::ast_ins=0;

AST::AST() {
	rootNode=Node::createNode();
	currentNode=rootNode;
	rootNode->token=TOKEN_NULL;
	rootNode->value=VALUE_PROGRAM_START;
}

AST* AST::getInstance() {
	return ast_ins==0?ast_ins=new AST():ast_ins;
}

void AST::output(FILE* file) {
	traversing(file, rootNode);
}

void AST::traversing(FILE *file, Node *node) {
	for (int i=0;i<node->depth;i++) {
		fprintf(file,"  ");
	}
	fprintf(file,"Depth:%d NodeType:%d  NodeToken:%d",node->depth,node->value,node->token);
	for (int i=0;i<node->name_args.size();i++) {
		fprintf(file, " %s", node->name_args[i].c_str());
	}
	fprintf(file,"\n");
	if (node->rightpointer)
		traversing(file,node->rightpointer);
	if (node->leftpointer)
		traversing(file,node->leftpointer);
}